import modelExtend from 'dva-model-extend'
import {lwjPageModel} from 'utils/model'
import {routerRedux} from 'dva/router';

export default modelExtend(lwjPageModel, {
  namespace: 'success',
  state: {
    areaText: '',
    listUrl: '',
    detailUrl: ''
  },
  subscriptions: {
    setup({dispatch, history}) {
      history.listen((location) => {
        if (location.pathname === "/success") {
          dispatch({
            type: 'setSuccessPage',
            payload: {
              areaText: location.state.areaText,
              listUrl: location.state.listUrl,
              detailUrl: location.state.detailUrl
            }
          })
        }
      })
    }
  },
  effects: {
    * backToList({
      payload
    }, {select, call, put}) {
      yield put(routerRedux.push(payload.listUrl));
    },
    * backToDetail({
      payload
    }, {select, call, put}) {
      yield put(routerRedux.push(payload.detailUrl));
    }
  },
  reducers: {
    setSuccessPage(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    }
  }
})
